package com.sjd.web.api.order;

import com.sjd.common.core.controller.BaseController;
import com.sjd.common.core.domain.AjaxResult;
import com.sjd.common.core.page.TableDataInfo;
import com.sjd.mall.domain.dto.base.OrderNoRequestDTO;
import com.sjd.mall.domain.dto.order.*;
import com.sjd.mall.service.order.WxOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * WxOrder
 *
 * @author seer
 * @version 1.0
 * @date 2022/1/21 09:36
 */
@Api(tags = "订单模块(小程序)")
@RestController
@RequestMapping(value = "/weixin/api/order")
@Slf4j
public class WxOrderApi extends BaseController {

    @Autowired
    private WxOrderService orderService;

    /**
     * 订单创建
     * 直接购买
     *
     * @param requestDTO requestDTO
     * @return object
     */
    @ApiOperation(value = "订单创建",
            response = WxOrderQryResponseDTO.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/create")
    public AjaxResult create(@Validated @RequestBody WxOrderCreateRequestDTO requestDTO) {
        log.info("订单创建(直接购买),请求 {}", requestDTO);
        AjaxResult result = orderService.create(requestDTO);
        log.info("订单创建(直接购买),响应 {}", result);
        return result;
    }

    /**
     * 订单创建
     * 购物车
     *
     * @param requestDTO requestDTO
     * @return object
     */
    @ApiOperation(value = "购物车订单创建")
    @PostMapping(value = "/createCart")
    public AjaxResult createCart(@Validated @RequestBody WxOrderCreateCartRequestDTO requestDTO) {
        log.info("订单创建(购物车),请求 {}", requestDTO);
        AjaxResult result = orderService.createCart(requestDTO);
        log.info("订单创建(购物车),响应 {}", result);
        return result;
    }

    /**
     * 订单支付
     *
     * @param requestDTO requestDTO
     * @return object
     */
    @ApiOperation(value = "订单支付",
            response = WxOrderQryResponseDTO.class)
    @PostMapping(value = "/pay")
    public AjaxResult pay(@Validated @RequestBody OrderNoRequestDTO requestDTO) {
        log.info("订单支付,请求 {}", requestDTO);
        AjaxResult result = orderService.pay(requestDTO);
        log.info("订单支付,响应 {}", result);
        return result;
    }

    /**
     * 订单查询
     *
     * @param requestDTO requestDTO
     * @return object
     */
    @ApiOperation(value = "订单查询",
            response = WxOrderQryResponseDTO.class)
    @GetMapping("/qry")
    private TableDataInfo qry(WxOrderQryRequestDTO requestDTO) {
        startPage();
        if (log.isDebugEnabled()) {
            log.debug("订单查询,请求 {}", requestDTO);
        }
        List<WxOrderQryResponseDTO> respList = orderService.qry(requestDTO);
        if (log.isDebugEnabled()) {
            log.debug("订单查询,响应 {}", respList);
        }
        return getDataTable(respList);
    }

    /**
     * 订单退款
     *
     * @param requestDTO requestDTO
     * @return object
     */
    @ApiOperation(value = "订单退款")
    @PostMapping(value = "/refund")
    public AjaxResult refund(@Validated @RequestBody OrderNoRequestDTO requestDTO) {
        log.info("订单退款,请求 {}", requestDTO);
        AjaxResult result = orderService.refund(requestDTO);
        log.info("订单退款,响应 {}", result);
        return result;
    }

    /**
     * 订单取消
     *
     * @param requestDTO requestDTO
     * @return object
     */
    @ApiOperation(value = "订单取消")
    @PostMapping(value = "/cancel")
    public AjaxResult cancel(@Validated @RequestBody OrderNoRequestDTO requestDTO) {
        log.info("订单取消,请求 {}", requestDTO);
        AjaxResult result = orderService.cancel(requestDTO.getOrderNo());
        log.info("订单取消,响应 {}", result);
        return result;
    }

    /**
     * 确认收货
     *
     * @param requestDTO requestDTO
     * @return object
     */
    @ApiOperation(value = "确认收货")
    @PostMapping(value = "/receipt")
    public AjaxResult receipt(@Validated @RequestBody OrderNoRequestDTO requestDTO) {
        log.info("确认收货,请求 {}", requestDTO);
        AjaxResult result = orderService.receipt(requestDTO);
        log.info("确认收货,响应 {}", result);
        return result;
    }

    /**
     * 快递查询
     *
     * @param requestDTO requestDTO
     * @return object
     */
    @ApiOperation(value = "快递查询",
            response = WxCourierQryResponseDTO.class)
    @GetMapping("/courierQry")
    public AjaxResult courierQry(OrderNoRequestDTO requestDTO) {
        if (log.isDebugEnabled()) {
            log.debug("快递查询,请求 {}", requestDTO);
        }
        AjaxResult result = orderService.courierQry(requestDTO);
        if (log.isDebugEnabled()) {
            log.debug("快递查询,响应 {}", result);
        }
        return result;
    }
}
